<!--这里是退货操作时用订单id查询出来的页面数据-->
<table border="0" cellspacing="0" cellpadding="0" class="table">
	<thead>
		<tr>
			<th>序号</th>
			<th>商品名称</th>
			<th>商品数量</th>
			<th>商品价格</th>
			<th>金额</th>
			<th>备注</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody id="tbl" th:hidden="${shoppingCartList}==null?true:false">
	<tr th:each="t,line : ${shoppingCartList}">
		<td><span th:text="${line.count}"></span></td>
		<td><span th:text="${t.market.commodity.name }"></span></td>
		<td><input th:value="${t.number }" width:50px disabled/></td>
		<td class="number" style='display: none'><span th:text="${t.number }"></span></td>
		<td><span th:text="${t.unitPrice }"></span></td>
		<td><span th:text="${t.price }"></span></td>
		<td><span th:text="${t.state}==0?'':'有退货记录'"></span></td>
		<td class="productId" style="display: none"><span th:text="${t.productId }"></span></td>
		<td>
			<a href="javascript:void(0);" class="table_btn table_edit edit_btn">
				<i class="fa fa-edit"></i>
				<span>编辑</span>
			</a>
		</td>
	</tr>
	</tbody>
	<tfoot>
		<tr>
		<td>共<span th:text="${shoppingCartList}==null?'':${shoppingCartList.size()}"></span>项商品</td>
		<td>交易时间：<span th:text="${#dates.format(cashInfo.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span></td>
		<td colspan="5">订单金额（￥）：<span th:text="${(cashInfo.orderMoney - money)}<0?${cashInfo.orderMoney}+'（原价：'+${money}+')':${cashInfo.orderMoney}"></span></td>
		</tr>
	</tfoot>
</table>
<script th:inline="javascript" type="text/javascript">
	//判断是否可以退单
	//时间不能超过三天
	var time = [[${cashInfo?.createTime ?:""}]];
	var nowTime = new Date();
	var orderTime = new Date(time);
	if((nowTime.getTime() - orderTime.getTime())>3*24*60*60*1000){
		$(".cont_box table").after("<div style='color: red'>此订单已超时，仅退三天内购买的订单!</div>");
		$(".cont_box table tbody .edit_btn").removeClass("table_edit").addClass("btn_disabled");
		// $(".cont_box table tbody").undelegate(".edit_btn","click");
	}else{
		//数量不能扣为负数，循环退单列表.剩余数量为0，直接禁用按钮
		var tab=document.getElementById("tbl");
		var rows=tab.rows;//rows.length获取表格的行数
		alert(rows.length);
		for(var i=0;i<rows.length;i++) { //遍历表格的行
			var number = $("table tbody tr:eq("+i+") .number").text();//商品购买数量
			var productId = $("table tbody tr:eq("+i+") .productId").text();//商品id
			var salesReturn = [[${salesReturnRecordList}]];//按订单id查询出来的销售退货列表
			var flag = 0;
			for(var z of salesReturn){
				//判断商品id所剩数量
				if(z.productId == productId){
					//计算订单内还剩多少数量
					var numberLast = number - z.number;
					// alert(numberLast);
					if(numberLast != 0){
						//编辑按钮
						// alert("ok");
						$(".cont_box table tbody tr:eq("+i+")").delegate(".edit_btn","click",function(){
							//数量解除禁用
							$(this).parent().parent().find("input").attr("disabled",false);
							$(this).after(
									"<a href='javascript:void(0);' class='table_btn table_cancel btn_back'>" +
									"<i class='fa fa-window-close'></i>" +
									"<span> 取消</span>" +
									"</a>");
							$(this).after(
									"<a href='javascript:void(0);' class='table_btn table_edit btn_success'>" +
									"<i class='fa fa-check-square-o'></i>" +
									"<span>提交</span>" +
									"</a>");
							$(this).remove();
						});
					}else{
						$(".cont_box table tbody tr:eq("+i+") td .edit_btn").removeClass("table_edit").addClass("btn_disabled");
					}
					flag = 1;//查询到了退单记录
					break;
				}
			}
			if(flag == 0){//不存在退单记录
				$(".cont_box table tbody tr:eq("+i+")").delegate(".edit_btn","click",function(){
					//数量解除禁用
					$(this).parent().parent().find("input").attr("disabled",false);
					$(this).after(
							"<a href='javascript:void(0);' class='table_btn table_cancel btn_back'>" +
							"<i class='fa fa-window-close'></i>" +
							"<span> 取消</span>" +
							"</a>");
					$(this).after(
							"<a href='javascript:void(0);' class='table_btn table_edit btn_success'>" +
							"<i class='fa fa-check-square-o'></i>" +
							"<span>提交</span>" +
							"</a>");
					$(this).remove();
				});
			}

		}

	}
</script>
<!-- 退单单项商品-->
<script th:inline="javascript" type="text/javascript">
	$(function() {
		$(".cont_box table tbody").delegate(".btn_success","click", function () {
			var number = $(this).parent().parent().find(".number").text();
			var numbernew = $(this).parent().parent().find("input").val();
			var productId = $(this).parent().prev().text();
			var salesReturn = [[${salesReturnRecordList}]];//按订单id查询出来的销售退货列表
			var returnNumber;
			for(var i of salesReturn){
				if(i.productId == productId){
					returnNumber = i.number;
				}
			}
			// alert(returnNumber);
			// alert(numbernew);
			// alert(number);
			if(numbernew > number || numbernew <=0){
				$.layerTip("error", "退货数量不能小于1且不能大于原订单数量！");
				return;
			}else if(numbernew>(number-returnNumber)){
				$.layerTip("error", "此订单商品已退货数量："+returnNumber+",剩余可退货数量："+(number-returnNumber));
				return;
			}
			//获取退货原因
			var reason = prompt("请输入退货原因", "");
			if (reason != null && reason != "") {
				var orderId = $(".main_box .cont_box .search_salesReturn .orderId").val();
				var productId = $(this).parent().prev().text();
				$.post(
						"/salesReturnModify",
						"number=" + numbernew + "&orderId=" + orderId + "&productId=" + productId + "&reason=" + reason,
						function (data) {
							if (data.returnState == 1) {
								// $.layerTip("success", "退单成功！");
								alert("退单成功!");
								window.location.href="/salesReturn";
							} else {
								// $.layerTip("error", "退单失败！");
								alert("退单失败!");
								window.location.href="/salesReturn";
							}
						}
				)
			} else {
				alert("退货原因不能为空");
			}
		});
		<!-- 取消按钮-->
		$(".cont_box table tbody").delegate(".btn_back","click", function () {
			$(this).parent().parent().find("input").attr("disabled", true);
			$(this).parent().parent().find("input").val($(this).parent().parent().find(".number").text());
			$(this).after(
					"<a href='javascript:void(0);' class='table_btn table_edit edit_btn'>" +
					"<i class='fa fa-edit'></i>" +
					"<span>编辑</span>" +
					"</a>"
			);
			$(this).prev().remove();
			$(this).remove();
		});
	});
</script>